
package hasthi.wsdm.capabilities;

import hasthi.common.HasthiException;
import hasthi.common.XmlBeanUtils;
import hasthi.common.HasthiException.FaultCode;

import java.util.Date;

import javax.xml.namespace.QName;

import org.apache.xmlbeans.XmlCalendar;
import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlObject;
import org.w3.x2005.x08.addressing.EndpointReferenceType;

import wsdm.mows.IdleStateDocument;
import wsdm.mows.StoppedStateDocument;
import wsdm.mows.UpStateDocument;
import wsdm.muws.ManagementEventDocument;
import wsdm.muws.ManagementEventType;
import wsdm.muws.OtherSituationDocument;
import wsdm.muws.SituationDocument;
import wsdm.muws.SituationType;
import wsdm.muws.StartCompletedDocument;
import wsdm.muws.StateTransitionDocument;
import wsdm.muws.StateTransitionType;
import wsdm.muws.StopInitiatedDocument;
import wsdm.muws.OtherSituationDocument.OtherSituation;
import wsdm.muws.SituationType.SuccessDisposition;
import edu.indiana.extreme.wsdm.ServiceDeploymentInformationDocument;
import edu.indiana.extreme.wsdm.ServiceErrorInformationDocument;
import edu.indiana.extreme.wsdm.ServiceDeploymentInformationDocument.ServiceDeploymentInformation;

public class RequestProcessingStateCapability extends AbstractCapability {
    //TODO define the events
    
    public void setProperty(QName name, Object value) throws HasthiException {
        throw new HasthiException("Unsupported property "+name + " set in capability "+ this,FaultCode.InvaliedLocalArgumnet);
    }
   
    public Object getProperty(QName name) throws HasthiException {
        throw new HasthiException("Unsupported property "+name + " requested from capability "+ this,FaultCode.InvaliedLocalArgumnet);
    }
    
    public void sendServiceInitializedEvent(EndpointReferenceType epr,QName serviceName,
            String wsdlUrl,String serviceHost,String serviceRootDir) throws HasthiException{
//        <ManagementEvent ReportTime="dateTime">
//        <EventId>anyURI</EventId>
//        <SourceComponent>
//                <ResourceId>anyURI</ResourceId>
//                <ComponentAddress>any/WS-Addressing Epr is recamnded</ComponentAddress>
//        </SourceComponent>
//        <ReporterComponent>Same as Source Component</ReporterComponent> <!-- optional -->
//        <muws2:Situation>
//                <muws2:SituationCategory>
//                        <StopSituation><StopInitiated/></StopSituation> <! page 10 MUWS-2-->
//                </muws2:SituationCategory>
//                <muws2:SuccessDisposition>(Successful|Unsuccessful)</muws2:SuccessDisposition> ?
//                <muws2:SituationTime>xs:dateTime</muws2:SituationTime> ?
//                <muws2:Priority>xs:short</muws2:Priority> ?
//                <muws2:Severity>xs:short</muws2:Severity> ?
//                <muws2:Message>muws:LangString</muws2:Message> ?
//                <muws2:SubstitutableMsg MsgId=”xs:string” MsgIdType=”xs:anyURI”>
//                        <muws2:Value>xs:anySimpleType</muws2:Value>*
//                </muws2:SubstitutableMsg> ?
//        </muws2:Situation>
//        <! MUWS 3.2 State -->
//        <StateTransition Time="xs:datetime" TransitionIdentifier="anyURI">
//                <EnteredState><exns:StoppedState/><EnteredState>
//        </StateTransition>
//
//        <ServiceInitializedNotification>
//                <wsdlUrl></wsdlUrl>
//                <serviceRootDir></serviceRootDir>
//                <serviceHost></serviceHost>
//        </ServiceInitializedNotification>
//</ManagementEvent>
        XmlCalendar currentTime = new XmlCalendar(new Date());
        ManagementEventDocument eventDocument = createBaseEvent(currentTime);
        ManagementEventType eventType = eventDocument.getManagementEvent();

        
        SituationDocument situationDocument = SituationDocument.Factory.newInstance();
        SituationType situationType = situationDocument.addNewSituation();
        StartCompletedDocument startCompletedDocument = StartCompletedDocument.Factory.newInstance();
        startCompletedDocument.addNewStartCompleted().addNewStartSituation();
        XmlBeanUtils.addElementAsChild(situationType.addNewSituationCategory(),startCompletedDocument);
        situationType.setSuccessDisposition(SuccessDisposition.SUCCESSFUL);
        situationType.setSituationTime(currentTime);
        //Pririty 0-100
        situationType.setPriority((short)80);
        //0-6 Fatal,Critical,Major,Minor,Warning,Information,Unknown
        situationType.setSeverity((short)1);
        
        XmlBeanUtils.addElementAsChild(eventType, startCompletedDocument);
        
        StateTransitionDocument stateTransitionDocument = StateTransitionDocument.Factory.newInstance();
        StateTransitionType stateTransition = stateTransitionDocument.addNewStateTransition();
        stateTransition.setTime(currentTime);
        IdleStateDocument idleStateDocument = IdleStateDocument.Factory.newInstance();
        idleStateDocument.addNewIdleState().addNewUpState();
        stateTransition.setEnteredState(idleStateDocument.getIdleState());
        
        XmlBeanUtils.addElementAsChild(eventType, stateTransitionDocument);
        
        ServiceDeploymentInformationDocument serviceDeploymentInformationDocument =  ServiceDeploymentInformationDocument.Factory.newInstance();
        ServiceDeploymentInformation serviceDeploymentInformation = serviceDeploymentInformationDocument.addNewServiceDeploymentInformation();
        serviceDeploymentInformation.setServiceHost(serviceHost);
        serviceDeploymentInformation.setServiceRootDir(serviceRootDir);
        serviceDeploymentInformation.setWsdlUrl(wsdlUrl);
        XmlBeanUtils.addElementAsChild(eventType, serviceDeploymentInformationDocument);
        sendEvent(eventDocument);
    }
    
    
    public void serviceShutDownEvent(EndpointReferenceType epr,QName serviceName) throws HasthiException{
      XmlCalendar currentTime = new XmlCalendar(new Date());
      ManagementEventDocument eventDocument = createBaseEvent(currentTime);
      ManagementEventType eventType = eventDocument.getManagementEvent();

      
      SituationDocument situationDocument = SituationDocument.Factory.newInstance();
      SituationType situationType = situationDocument.addNewSituation();
      
      StopInitiatedDocument stopInitiatedDocument = StopInitiatedDocument.Factory.newInstance();
      stopInitiatedDocument.addNewStopInitiated().addNewStopSituation();
      situationType.setSituationCategory(stopInitiatedDocument.getStopInitiated());
      
      situationType.setSuccessDisposition(SuccessDisposition.SUCCESSFUL);
      situationType.setSituationTime(currentTime);
      //Pririty 0-100
      situationType.setPriority((short)80);
      //0-6 Fatal,Critical,Major,Minor,Warning,Information,Unknown
      situationType.setSeverity((short)1);
      
      XmlBeanUtils.addElementAsChild(eventType, situationDocument);
      
      StateTransitionDocument stateTransitionDocument = StateTransitionDocument.Factory.newInstance();
      StateTransitionType stateTransition = stateTransitionDocument.addNewStateTransition();
      stateTransition.setTime(currentTime);
      stateTransition.setPreviousState(UpStateDocument.Factory.newInstance().addNewUpState());
      
      StoppedStateDocument stoppedStateDocument = StoppedStateDocument.Factory.newInstance();
      stoppedStateDocument.addNewStoppedState().addNewDownState();
      stateTransition.setEnteredState(stoppedStateDocument.getStoppedState());
      
      
      XmlBeanUtils.addElementAsChild(eventType, stateTransitionDocument);
      sendEvent(eventDocument);
  }
    
    public void sendServiceErrorEvent(EndpointReferenceType epr,QName serviceName,HasthiException exception) throws HasthiException{
        try {
            XmlCalendar currentTime = new XmlCalendar(new Date());
            ManagementEventDocument eventDocument = createBaseEvent(currentTime);
            ManagementEventType eventType = eventDocument.getManagementEvent();

            
            SituationDocument situationDocument = SituationDocument.Factory.newInstance();
            SituationType situationType = situationDocument.addNewSituation();
            
            OtherSituationDocument otherSituationDocument = OtherSituationDocument.Factory.newInstance();
            OtherSituation otherSituation = otherSituationDocument.addNewOtherSituation();
            XmlObject param = XmlObject.Factory.parse("<ErrorOccured/>"); 
            XmlBeanUtils.addElementAsChild(otherSituation, param);
            situationType.setSituationCategory(otherSituation);
            
            situationType.setSuccessDisposition(SuccessDisposition.SUCCESSFUL);
            situationType.setSituationTime(currentTime);
            //Pririty 0-100
            situationType.setPriority((short)50);
            //0-6 Fatal,Critical,Major,Minor,Warning,Information,Unknown
            situationType.setSeverity((short)1);
            
            XmlBeanUtils.addElementAsChild(eventType, situationDocument);
            
            
            ServiceErrorInformationDocument serviceErrorInformationDocument = exception.getErrorAsXml();
            XmlBeanUtils.addElementAsChild(eventType, serviceErrorInformationDocument);
            sendEvent(eventDocument);
        } catch (XmlException e) {
            throw new HasthiException(e, FaultCode.LocalError);
        }
    }
    
    
    public static void main(String[] args){
        
    }
}

